/* Message class define a set of messages sent between the chatroom server
 *      and  chatroom visitors.
 *  Data members:
 *  	. type    : an integer to indicate kind of message
 *  	. message : a message sent out to each visitor including the sender.
 * 	. visitName : a string contains one or more visitor names. 
*/

// package chatroom;

import java.io.*;
import java.util.*;


public class Message implements Serializable {

    public static final int
	PUBLIC = 0, // Message: (PUBLIC, "message", null).
		     // Client : sends to server, or receives it and append to it
		     //          text area.
		     // Server : receives the message broadcasters it very visitor
		     //          without any change.
		   
	PRIVATE = 1, // Message: (PRIVATE, "message", "name1; name2. ..")
		     // Client : writes this type of message(1, message, visitor_list)
		     //		 to server.
		     // Server : receives the message and visitor list
		     //          makes a vector of visitors, and make a general message
		     //             as (0, "private to name1, name2m, ...", null).
		     //          broadcasts above message to each visitor whose name
		     //          appeard in the list.
	LOGIN   = 2, // Message: (LOGIN, null, name)
		     // Client : sends the message.
		     //          visitor enters his/her name in the GUI interface.
		     // Server : Add Client (name, null, OutputStream) to its visitor list.
		     //          broadcasts the message to each visitor.
		     // Client : Receives the message, and add Client (name, false, null)
		     //          to its visitor list. Append message on who is login to its
		     //          board. Repaint name list.
	LOGOUT  = 3; // Message: (LOGOUT, null, name)
		     // Client : send a message of format above and walk out.
    		     // Server : Server agent detects the logout, sever removes the
		     //          the visitor from its visitor list;
		     //          broadcasts the message to every visitor.
		     // Client : Removes Visitor(name, any, null) from its name list.
		     // 	 Repaint name list.
		     
    public static final String
	NAME_SEPARATOR = ";"; // string used to separate visitor names. 
    public int 		type 	    = PUBLIC;	// type of message defined above

    // message contains message to receivers, or name if message type is
    // eiehr LOGIN or LOGOUT type.
    public String	message     = ""; 


    // hold list of receicer names. If it is null or empty, all visitors
    // receivers.
    public String       receivers = "";   // null, one or more visitor names.

    public  Message( ) { set ( PUBLIC, "invalid", ""); }
    public  Message( Message m ) {
	set( m.type, m.message, m.receivers);
    }
    public  Message(int t, String msg, String vName) { set( t, msg, vName ); }

    public  void set(int t, String msg, String vName) {
	type = t;  message = new String (msg); receivers = new String( vName);

    }

    public  String toString() {
	return String.format("%3d %20s %s", type, message, receivers);
    }

    public  Vector<String> getReceiverNames() { 
	if ( receivers == null ) return null;
	Vector<String> v = new Vector<String>( 10 );
	if ( receivers.trim().equals("") ) return v;
	else {
	    StringTokenizer token = new StringTokenizer( receivers, NAME_SEPARATOR);
	    while ( token.hasMoreTokens() ) v.add( token.nextToken() );
	}
	Collections.sort(v);
	return v;
    }
}
